package com.lei.zktwo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lei.mvptwozk.mvp.view.BaseActivty;
import com.lei.zktwo.mvp.contract.LoginContract;
import com.lei.zktwo.mvp.model.LoginModel;
import com.lei.zktwo.mvp.presenter.LoginPresenter;

public class MainActivity extends BaseActivty<LoginPresenter> implements LoginContract.AppKeyView {
    private LoginPresenter loginPresenter;
    private EditText etNameAppkey;
    private EditText etPasswdAppkey;
    private EditText etEmailAppkey;
    private Button btAppkey;

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences log = getSharedPreferences("log", MODE_PRIVATE);
        boolean login = log.getBoolean("login", false);
        if (login){
            startActivity(new Intent(MainActivity.this,ZhuActivity.class));
        }
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initview() {
        etNameAppkey = (EditText) findViewById(R.id.et_name_appkey);
        etPasswdAppkey = (EditText) findViewById(R.id.et_passwd_appkey);
        etEmailAppkey = (EditText) findViewById(R.id.et_email_appkey);
        btAppkey = (Button) findViewById(R.id.bt_appkey);
        btAppkey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = etNameAppkey.getText().toString().trim();
                String passwd = etPasswdAppkey.getText().toString().trim();
                String email = etEmailAppkey.getText().toString().trim();
                if (name!=null&&passwd!=null&&email!=null){
                    loginPresenter.Appkey();
                }
            }
        });
    }

    @Override
    public void inject() {
        loginPresenter = new LoginPresenter(new LoginModel(),this);
    }

    @Override
    public String getname() {
        return etNameAppkey.getText().toString().trim();
    }

    @Override
    public String getpasswd() {
        return etPasswdAppkey.getText().toString().trim();
    }

    @Override
    public String getemail() {
        return etEmailAppkey.getText().toString().trim();
    }

    @Override
    public void gomain(String json) {
        if (!json.equals(null)){
            Toast.makeText(this, "获取到的AppKey为:"+json, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.putExtra("appkey",json);
            startActivity(intent);
            finish();
        }
    }
}
